<?php
// +----------------------------------------------------------------------
// | Action ：论坛
// +----------------------------------------------------------------------
// | DateTime：2015年12月12日
// +----------------------------------------------------------------------
// | Author: Danson <zoudan916@163.com>
// +----------------------------------------------------------------------

namespace Home\Controller;

class ForumController extends WapController{

    public function action(){
        return 'Index/index';
    }

    /* 评论列表 */
    public function index(){
        $count = M('comment')->count();
        $list = M('comment')->limit(0,10)->order('comment_id desc')->select();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->display();
    }

    /* 添加评论 */
    public function save(){
        $result['user_name'] = $_SESSION['wx_user']['nickname'];
        $result['user_img'] = $_SESSION['wx_user']['headimgurl'];
        $result['openid'] = $_SESSION['wx_user']['openid'];
        $result['content'] = I('post.content');
        $result['check'] = 0;
        $result['create_time'] = time();
        $id = M('comment')->add($result);
        if($id){
            $info = M('comment')->where(array('comment_id'=>$id))->find();
            $info['status'] = 1;
            $this->ajaxReturn($info);
        }else{
            $this->ajaxReturn(array('status'=>0));
        }
    }

    /* 动态加载 */
    public function ajaxRulset(){
        $_page = I('post.page');
        $list = M('comment')->limit($_page,10)->order('comment_id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time'] = time_format('Y-m-d',$v['create_time']);
        }
        echo json_encode($list);
    }
}